import {
  homeImages, homeTexts, homeTagList, homeHotList, homeNoticeDetail, homeNavList, homeMoreBodyList
} from '@/services'
const state = {
  // 图片广告数据
  images: [],
  // 公告轮播数据
  texts: [],
  // 热卖标题数据
  tagList: [],
  // 热卖数据
  hotList: [],
  // 更多数据
  moreList: [],
  // 公告详情数据
  noticeDetail: {},
  // 商城热卖更多数据
  lastedProdPagelist: {},
};

const getters = {
};

const mutations = {
  update(state, payload) {
    state.images = payload;
  },
  getImages(state, payload) {
    state.images = payload;
  },
  getTexts(state, payload) {
    state.texts = payload;
  },
  getTagList(state, payload) {
    state.tagList = payload;
  },
  getHotList(state, payload) {
    state.hotList = payload.records;
  },
  getMoreList(state, payload) {
    state.moreList = payload.records
  },
  getNoticeDetail(state, payload) {
    state.noticeDetail = payload
  },
  getMoreBodyList(state, payload) {
    state.lastedProdPagelist = payload
  },
  getWrong(state, payload) {
    state.lastedProdPagelist = payload
  },
  getHomeNavList(state, payload) {
    if (payload.current == 1) {
      state.lastedProdPagelist = payload
    } else {
      payload.records.forEach((item) => {
        state.lastedProdPagelist.records.push(item)
      })
    }
  }
};

const actions = {
  async homeImages({ commit }, payload) {
    let result = await homeImages(payload);
    if (result) {
      commit("getImages", result);
    }
  },
  async homeTexts({ commit }, payload) {
    let result = await homeTexts(payload);
    if (result) {
      commit("getTexts", result);
    }
  },
  async homeTagList({ commit }, payload) {
    let result = await homeTagList(payload);
    if (result) {
      commit("getTagList", result);
    }
  },
  async homeHotList({ commit }, payload) {
    let result = await homeHotList(payload);
    if (result) {
      commit("getHotList", result);
    }
  },
  async homeMoreList({ commit }, payload) {
    let result = await homeHotList(payload);
    if (result) {
      commit("getMoreList", result);
    }
  },
  async homeNoticeDetail({ commit }, payload) {
    let result = await homeNoticeDetail(payload);
    if (result) {
      commit('getNoticeDetail', result);
    }
  },
  async homeMoreBodyList({ commit }, payload) {
    let result = await homeMoreBodyList(payload);
    if (result) {
      commit('getMoreBodyList', result);
    }
  },
  async homeNavList({ commit }, payload) {
    let result
    try {
      result = await homeNavList(payload)
    } catch (err) {
      commit('getWrong', {})
    }
    if (result) {
      commit('getHomeNavList', result)
    }
  }
};

export default {
  namespaced: true,
  modules: {},
  state,
  getters,
  mutations,
  actions,
};